package com.dd.logistics.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.logistics.common.Result;
import com.dd.logistics.entity.OutboundOrder;
import com.dd.logistics.service.OutboundOrderService;
import com.dd.logistics.utils.OrderNumberGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/outbound")
public class OutboundOrderController {
    
    @Autowired
    private OutboundOrderService outboundOrderService;
    
    @GetMapping("/list")
    public Result<List<OutboundOrder>> list() {
        try {
            List<OutboundOrder> list = outboundOrderService.getOutboundOrderList();
            return Result.success(list);
        } catch (Exception e) {
            return Result.error("获取出库单列表失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/page")
    public Result<Page<OutboundOrder>> page(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        try {
            Page<OutboundOrder> page = outboundOrderService.getOutboundOrderPage(current, size);
            return Result.success(page);
        } catch (Exception e) {
            return Result.error("分页查询出库单失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/create")
    public Result<Boolean> createOutboundOrder(@RequestBody OutboundOrder outboundOrder) {
        try {
            if (outboundOrder.getCustomerId() == null || outboundOrder.getProductCode() == null) {
                return Result.error("客户ID和商品编码不能为空");
            }
            if (outboundOrder.getQuantity() == null || outboundOrder.getQuantity() <= 0) {
                return Result.error("出库数量必须大于0");
            }
            
            // 自动生成出库单号
            outboundOrder.setOrderNumber(OrderNumberGenerator.generateOutboundNumber());
            // 设置初始状态
            outboundOrder.setStatus(OutboundOrder.STATUS_PENDING);
            
            boolean success = outboundOrderService.createOutboundOrder(outboundOrder);
            return success ? Result.success("创建出库单成功", true) : Result.error("创建出库单失败，库存不足");
        } catch (Exception e) {
            return Result.error("创建出库单失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/picking/{orderNumber}")
    public Result<Boolean> confirmPicking(@PathVariable String orderNumber) {
        try {
            if (orderNumber == null || orderNumber.trim().isEmpty()) {
                return Result.error("出库单号不能为空");
            }
            
            boolean success = outboundOrderService.confirmPicking(orderNumber.trim());
            return success ? Result.success("确认拣货成功", true) : Result.error("确认拣货失败");
        } catch (Exception e) {
            return Result.error("确认拣货失败：" + e.getMessage());
        }
    }
    
    @PostMapping("/confirm/{orderNumber}")
    public Result<Boolean> confirmOutbound(@PathVariable String orderNumber) {
        try {
            if (orderNumber == null || orderNumber.trim().isEmpty()) {
                return Result.error("出库单号不能为空");
            }
            
            boolean success = outboundOrderService.confirmOutbound(orderNumber.trim());
            return success ? Result.success("确认出库成功", true) : Result.error("确认出库失败");
        } catch (Exception e) {
            return Result.error("确认出库失败：" + e.getMessage());
        }
    }
    
    @GetMapping("/detail/{orderNumber}")
    public Result<OutboundOrder> getDetail(@PathVariable String orderNumber) {
        try {
            OutboundOrder order = outboundOrderService.getByOrderNumber(orderNumber);
            return order != null ? Result.success(order) : Result.error("出库单不存在");
        } catch (Exception e) {
            return Result.error("获取出库单详情失败：" + e.getMessage());
        }
    }
} 